//
//  HTTripDeatillViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "HTTripDeatillViewController.h"
#import "HTTripDeatilTableViewCell.h"
#import "PKRequestManager.h"
#import "HTTripDeattillCommentModel.h"
#import "HTTripDeatillCommentHeaderViewController.h"
#import "HTTRIPDeatillHeaderModel.h"
#import "MJRefresh.h"
#import "HTTripDeatillCommentViewController.h"
@interface HTTripDeatillViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    int _page;
    
    int _addMorePage;
}
@property (nonatomic, strong) NSMutableArray *commentArr;//装评论的数组
@property (nonatomic, strong) HTTripDeattillCommentModel *commentModel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HTTRIPDeatillHeaderModel *headerModel;
@property (nonatomic, strong) NSMutableArray *headerArr;//装表头的数组
@property (nonatomic, strong) HTTripDeatillCommentHeaderViewController *headerVC;
@end

@implementation HTTripDeatillViewController

- (NSMutableArray *)commentArr
{
    if (!_commentArr) {
        _commentArr = [NSMutableArray array];
    }
    return _commentArr;
}

- (NSMutableArray *)headerArr
{
    if (!_headerArr) {
        _headerArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _headerArr;
}


- (void)requestData:(NSString *)experience page:(int)page
{
    NSString *pagestr = [NSString stringWithFormat:@"%d",page];
    NSString *newUrl = [self experienceid:experience page:pagestr];
    
    [PKRequestManager requestWithType:GET urlString:newUrl parDic:nil finish:^(NSData *data) {
        //评论加载
        if (page == _page) {
            [self.commentArr removeAllObjects];
        }
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
        for (NSDictionary *key in dic[@"responseData"][@"list"])
        {
            _commentModel = [[HTTripDeattillCommentModel alloc]init];
            
            [_commentModel setValuesForKeysWithDictionary:key];
            
            [self.commentArr addObject:_commentModel];
        }
        
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"践行详情评论加载失败");
    }];
}

- (void)headerRequestData
{
    //防止_experience_id为空在进行字符串拼接的时候照成程序崩溃
    if (_experience_id == nil) {
        return;
    }
    
    NSString *str = [[headerURL stringByAppendingString:_experience_id]stringByAppendingString:@"&uid="];
    
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *Headerdic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
        
        NSDictionary *head = Headerdic[@"responseData"];

        _headerModel = [[HTTRIPDeatillHeaderModel alloc]init];
        [_headerModel setValuesForKeysWithDictionary:head];
        
        [_headerVC setHeaderModel:_headerModel];
        
    } error:^(NSError *error) {
        NSLog(@"践行表头加载失败%@",error);
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _addMorePage = 2;
    [self requestData:_experience_id page:_page];
    
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,70, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 158;
    [self.view addSubview:_tableView];
    
    //表头
    _headerVC = [[HTTripDeatillCommentHeaderViewController alloc]init];
    _headerVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 580);
    _tableView.tableHeaderView = _headerVC.view;
    
    [self headerRequestData];
    
    //与nib文件关联
    [_tableView registerNib:[UINib nibWithNibName:@"HTTripDeatilTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"reuser"];
    
    //设置返回按钮
    
    UIButton *returnButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    returnButton.frame = CGRectMake(10, 15, 20, 20);
    [returnButton setImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    
    [returnButton addTarget:self action:@selector(doreturn) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:returnButton];
    
    //上提加载更多
    [self addmoreData];
    
    //下拉刷新
    [self refresh];
}
 //上提加载更多
- (void)addmoreData
{
    __weak HTTripDeatillViewController *deatillVC = self;
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [deatillVC requestData:_experience_id page:_addMorePage];
        [_tableView.footer endRefreshing];
        _addMorePage ++;
    } ];
    
}
//下拉刷新
- (void)refresh
{
    __weak HTTripDeatillViewController *deatillVC = self;
    
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [deatillVC requestData:_experience_id page:_page];
        [_tableView.header endRefreshing];
    }];
}


- (void)doreturn
{

    [self.navigationController popViewControllerAnimated:YES];
}


//拼接URL后面的参数
- (NSString *)experienceid:(NSString *)experienceid page:(NSString *)page
{
    //防止_experience_id为空在进行字符串拼接的时候照成程序崩溃
    if (experienceid == nil) {
        return @"";
    }

    NSString *str = [[[[commentURL stringByAppendingString:experienceid]stringByAppendingString:@"&page="]stringByAppendingString:page]stringByAppendingString:@"&uid="];
    return str;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _commentArr.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
    [cell setNeedsLayout];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuser";
    
    HTTripDeatilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str forIndexPath:indexPath];
    
    cell.commentMD = _commentArr[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HTTripDeattillCommentModel *model = [[HTTripDeattillCommentModel alloc]init];
    model = self.commentArr[indexPath.row];
    return [HTTripDeatilTableViewCell heightForComment:model];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HTTripDeatillCommentViewController *deatillCVC = [[HTTripDeatillCommentViewController alloc]init];
   
    deatillCVC.commentModel = _commentArr[indexPath.row];
    
    [self.navigationController pushViewController:deatillCVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
